import datetime
import json
import time

# Create your tests here.
from requests.structures import CaseInsensitiveDict

from util.HttpUtil.BaseHttpUtil import BaseHttpUtil
from util.HttpUtil.EncodeingUtil import url_encode
#添加活动模板
def createActivityTemplate(token,activityId,templateId,couponCost):
    host = "api.aiyouzhan.cn"
    headers = CaseInsensitiveDict([
        ('Content-Type', 'application/json;charset=UTF-8'),
        ('Access-Control-Allow-Headers',
         'Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers'),
        ('Connection', 'keep-alive'),
        ('Host', host),
        ('Authorization', "Bearer %s" % token),
        ('User-Agent',
         "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36")])

    spider = BaseHttpUtil(host=host)
    data = json.dumps({
        "id": "",
        "templateId":templateId ,
        "couponCost": couponCost,
        "description": "加汽油满%s使用！" % (couponCost),
        "quantity": "1",
        "allowGiveAway": "",
        "maxActivePerUser": "",
        "activityId": activityId,
        "token": token,
    })
    print(data)
    rep = spider.post(url="https://api.aiyouzhan.cn/api/coupon/enterprise/activityTemplate/create?token=%s&t=%s" % (
    token, int(time.time())), data=data, headers=headers)
    print(rep.json())
#删除活动模板
def delActivityTemplate(token,id):
    host = "api.aiyouzhan.cn"

    spider = BaseHttpUtil(host=host)
    data = url_encode({
        "id": id,
        "token": token,
    })
    print(data)
    rep = spider.post(url="https://api.aiyouzhan.cn/api/coupon/enterprise/activityTemplate/delete?t=%s" % (
         int(time.time())), data=data, )
    print(rep.json())

def createActivityTemplates(token,activityId,):
    createActivityTemplate(token=token, activityId=activityId, templateId="1173528364460302337", couponCost="100")
    createActivityTemplate(token=token, activityId=activityId, templateId="1173528406881492994", couponCost="200")
    createActivityTemplate(token=token, activityId=activityId, templateId="1173528437793513473", couponCost="300")
    createActivityTemplate(token=token, activityId=activityId, templateId="1173528470773325825", couponCost="400")
    createActivityTemplate(token=token, activityId=activityId, templateId="1173528505309224961", couponCost="500")


#添加活动
def createActivity(day,token):


    host ="api.aiyouzhan.cn"
    headers = CaseInsensitiveDict([
                ('Content-Type', 'application/json;charset=UTF-8'),
                ('Access-Control-Allow-Headers','Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers'),
                ('Connection', 'keep-alive'),
                ('Host', host),
                ('User-Agent', "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"),
        ('Authorization',"Bearer %s" % token),

    ])

    spider = BaseHttpUtil(host=host)
    day1 = day + datetime.timedelta(days=-2)
    couponStartTime = (day).strftime("%Y-%m-%d")+' 00:00:00'
    couponEndTime = (day).strftime("%Y-%m-%d")+' 23:59:59'
    startTime = (day1).strftime("%Y-%m-%d")+' 00:00:00'
    endTime = (day).strftime("%Y-%m-%d")+' 23:59:59'
    name =  (day).strftime("%Y-%m-%d")+'会员日'
    data = json.dumps({
        "payMethod": "5",
        "expiry": "1",
        "couponStartTime": couponStartTime,
        "couponEndTime": couponEndTime,
        "activeDay": "",
        "delayDay": "",
        "given": "2",
        "mutualUse": "0",
        "maxPerDay": "1",
        "maxPerUser": "",
        "limitTimes": "",
        "limitDays": "",
        "limitWeeks": "一,二,三,四,五,六",
        "memberLevel": "",
        "allowPayMethods": "1,2,4,5",
        "id": "",
        "name": name,
        "type": "6",
        "amountType": "1",
        "minAmount": "",
        "maxAmount": "",
        "minArriveAmount": "",
        "maxArriveAmount": "",
        "giveOilType": "",
        "giveOilTypeNames": "",
        "needMember": "0",
        "startTime": startTime,
        "endTime": endTime,
        "oilTypeIds": "1165865085030129665,1165865139455418370",
        "oilTypeNames": "92#,95#",
        "description": "",
        "limitDaysJoinTimes": "",
        "limitWeeksJoinTimes": "",
        "limitMonthJoinTimes": "",
        "limitJoinTimes": "",
        "token": token
    })
    print(data)

    activityPostUrl = "https://api.aiyouzhan.cn/api/coupon/enterprise/activity/create?token=%s&t=%s" %(token,int(time.time()))
    print(activityPostUrl)

    rep = spider.post(url=activityPostUrl,data=data,headers=headers)
    print(rep.json())
    json1= rep.json()
    if (json1['code']==1):
        activityId = json1['data']
        createActivityTemplates(token=token,activityId=activityId)

#长坪员加油站的批量创建活动
def main():
    day =datetime.datetime.strptime("2020-4-19", '%Y-%m-%d')

    endDay= datetime.datetime.strptime("2021-1-1", '%Y-%m-%d')
    token ="1246585616712396800"
    while day<= endDay:
        print((day).strftime("%Y-%m-%d"))
        createActivity(day, token)
        day = day + datetime.timedelta(days=7)




if __name__ == '__main__':
    main()


